<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="接口编码" prop="inteCode">
        <el-input
          v-model="queryParams.inteCode"
          placeholder="请输入接口编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接口名称" prop="inteName">
        <el-input
          v-model="queryParams.inteName"
          placeholder="请输入接口名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接口地址" prop="inteUrl">
        <el-input
          v-model="queryParams.inteUrl"
          placeholder="请输入接口地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['integrate:integrate:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['integrate:integrate:edit']"
          >编辑</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['integrate:integrate:remove']"
          >删除</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['integrate:integrate:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="integrateList"
      border
      :max-height="550"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="接口编码" align="center" prop="inteCode" />
      <el-table-column label="接口名称" align="center" prop="inteName" />
      <el-table-column label="接口地址" align="center" prop="inteUrl" />
      <el-table-column label="接口类型" align="center" prop="inteType" />
      <el-table-column label="请求方式" align="center" prop="requestType" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            icon="Edit"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['integrate:integrate:edit']"
            >编辑</el-button
          >
          <el-button
            link
            icon="Delete"
            type="primary"
            @click="handleDelete(scope.row)"
            v-hasPermi="['integrate:integrate:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接口配置对话框 -->
    <el-dialog :title="title" v-model="open" :close-on-click-modal="false" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="接口编码" prop="inteCode">
          <el-input v-model="form.inteCode" placeholder="请输入接口编码" />
        </el-form-item>
        <el-form-item label="接口名称" prop="inteName">
          <el-input v-model="form.inteName" placeholder="请输入接口名称" />
        </el-form-item>
        <el-form-item label="接口地址" prop="inteUrl">
          <el-input v-model="form.inteUrl" placeholder="请输入接口地址" />
        </el-form-item>
        <el-form-item label="接口类型" prop="inteType">
          <el-select
            v-model="form.inteType"
            placeholder="请选择业务类型"
            clearable filterable
            style="width: 215px"
          >
            <el-option
              v-for="dict in interface_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="接口方式" prop="requestType">
          <el-select
            v-model="form.requestType"
            placeholder="请选择业务类型"
            clearable filterable
            style="width: 215px"
          >
            <el-option
              v-for="dict in request_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listIntegrate,
  getIntegrate,
  updateIntegrate,
  addIntegrate,
  delIntegrate,
} from "@/api/system/integrate";

export default {
  name: "Integrate",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接口配置表格数据
      integrateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inteCode: null,
        inteName: null,
        inteUrl: null,
        inteType: null,
        requestType: null,
        username: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        inteCode: [
          { required: true, message: "接口编码不能为空", trigger: "blur" },
        ],
        inteName: [
          { required: true, message: "接口名称不能为空", trigger: "blur" },
        ],
        inteUrl: [
          { required: true, message: "接口地址不能为空", trigger: "blur" },
        ],
        inteType: [
          { required: true, message: "接口类型不能为空", trigger: "change" },
        ],
        requestType: [
          { required: true, message: "请求方式不能为空", trigger: "change" },
        ],
      },
      interface_type:[],
      request_type:[]
    };
  },
  created() {
    const { proxy } = getCurrentInstance();
    const { interface_type,request_type } = proxy.useDict("interface_type","request_type");

    this.interface_type = interface_type;
    this.request_type = request_type;
    this.getList();
  },
  methods: {
    /** 查询接口配置列表 */
    getList() {
      this.loading = true;
      listIntegrate(this.queryParams).then((response) => {
        this.integrateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        inteCode: null,
        inteName: null,
        inteUrl: null,
        inteType: null,
        requestType: null,
        username: null,
        password: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加接口配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getIntegrate(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑接口配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateIntegrate(this.form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIntegrate(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除接口配置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delIntegrate(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "integrate/integrate/export",
        {
          ...this.queryParams,
        },
        `integrate_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
